<template>
  <div>
    <el-form
      :inline="true"
      :model="searchStudentVo"
      class="demo-form-inline"
      style="margin-top: 25px; margin-left: 20%"
    >
      <el-form-item label="年级">
        <el-input
          v-model="searchStudentVo.grade"
          placeholder="请输入学生的年级"
        ></el-input>
      </el-form-item>
      <el-form-item label="专业">
        <el-input
          v-model="searchStudentVo.major"
          placeholder="请输入学生的专业"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="searchStudent(currentPage, limit, searchStudentVo)"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item style="flow: right">
        <router-link :to="'/admin/student/add'">
          <el-button type="success">添加</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <el-table :data="studentList" border style="width: 100%">
      <el-table-column prop="studentid" label="学号" width="150" align="center">
      </el-table-column>
      <el-table-column label="头像" width="120" align="center">
        <template slot-scope="scope">
          <img
            v-if="scope.row.avartarurl != null"
            :src="scope.row.avartarurl"
          />
          <img v-if="scope.row.avartarurl == null" src="@/assets/未登录.png" />
        </template>
      </el-table-column>
      <el-table-column label="权限" width="100" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.isadmin === 1">管理员</div>
          <div v-if="scope.row.isadmin === 0">普通用户</div>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100" align="center">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120" align="center">
      </el-table-column>
      <el-table-column label="性别" width="50" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.sex === 0">男</div>
          <div v-if="scope.row.sex === 1">女</div>
          <div v-if="scope.row.sex === null">无</div>
        </template>
      </el-table-column>
      <el-table-column prop="grade" label="年级" width="75" align="center">
      </el-table-column>
      <el-table-column prop="major" label="专业" width="150" align="center">
      </el-table-column>
      <el-table-column label="状态" width="75" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.states === 1">正常</div>
          <div v-if="scope.row.states === 0" style="color: red">已禁言</div>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="175" align="center">
      </el-table-column>
      <el-table-column prop="qq" label="qq" width="120" align="center">
      </el-table-column>
      <el-table-column
        prop="telephone"
        label="联系电话"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column label="违规次数" width="50" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.illegalcount <= 5">
            {{ scope.row.illegalcount }}
          </div>
          <div v-if="scope.row.illegalcount > 5" style="color: red">
            {{ scope.row.illegalcount }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350" align="center">
        <template slot-scope="scope">
          <el-button
            type="danger"
            v-if="scope.row.states === 1"
            icon="el-icon-turn-off"
            @click="banStudent(scope.row.studentid)"
            >禁言</el-button
          >

          <el-button
            type="success"
            v-if="scope.row.states === 0"
            icon="el-icon-open"
            @click="cancelBan(scope.row.studentid)"
            >解禁</el-button
          >
          <el-button
            type="danger"
            v-if="scope.row.isadmin === 1"
            icon="el-icon-turn-off"
            @click="changeToNormal(scope.row.studentid)"
            >收回权限</el-button
          >
          <el-button
            type="success"
            v-if="scope.row.isadmin === 0"
            @click="changeToAdmin(scope.row.studentid)"
            icon="el-icon-open"
            >赋予权限</el-button
          >
          <el-button type="primary" icon="el-icon-chat-dot-round"
            >私聊</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :total="totalRecords"
      :page-size="limit"
      :page-sizes="[4, 8, 12, 16, 20, 24, 28]"
      style="margin: 30px 0; text-align: center"
      layout="sizes,prev,pager,next,jumper,->,total,slot"
      @current-change="changeCurrentPage"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import adminControll from "@/api/adminControll";
export default {
  data() {
    return {
      searchStudentVo: {
        grade: "",
        major: "",
      },
      currentPage: 1,
      limit: 4,
      totalRecords: 100,
      studentList: [
        {
          studentid: 202003150624,
          isadmin: 1,
          avartarUrl:
            "https://yygh-zjut.oss-cn-hangzhou.aliyuncs.com/76f62cf207906149ba614837eee3ae6e.jpg",
          nickname: "Xzh",
          name: "徐振华",
          sex: "男",
          grade: "2020",
          major: "数字媒体与技术",
          states: 0,
          email: "718457605@qq.com",
          qq: "718457605",
          telephone: "18767175883",
          illegalcount: 6,
        },
        {
          studentid: 202003150624,
          isadmin: 0,
          avartarUrl:
            "https://yygh-zjut.oss-cn-hangzhou.aliyuncs.com/76f62cf207906149ba614837eee3ae6e.jpg",
          nickname: "Xzh",
          name: "徐振华",
          sex: "男",
          grade: "2020",
          major: "数字媒体与技术",
          states: 1,
          email: "718457605@qq.com",
          qq: "718457605",
          telephone: "18767175883",
          illegalcount: 3,
        },
        {
          studentid: 202003150624,
          isadmin: 0,
          avartarUrl:
            "https://yygh-zjut.oss-cn-hangzhou.aliyuncs.com/76f62cf207906149ba614837eee3ae6e.jpg",
          nickname: "Xzh",
          name: "徐振华",
          sex: "男",
          grade: "2020",
          major: "数字媒体与技术",
          states: 0,
          email: "718457605@qq.com",
          qq: "718457605",
          telephone: "18767175883",
          illegalcount: 6,
        },
        {
          studentid: 202003150624,
          isadmin: 0,
          avartarUrl:
            "https://yygh-zjut.oss-cn-hangzhou.aliyuncs.com/76f62cf207906149ba614837eee3ae6e.jpg",
          nickname: "Xzh",
          name: "徐振华",
          sex: "男",
          grade: "2020",
          major: "数字媒体与技术",
          states: 0,
          email: "718457605@qq.com",
          qq: "718457605",
          telephone: "18767175883",
          illegalcount: 6,
        },
        {
          studentid: 202003150624,
          isadmin: 0,
          avartarUrl:
            "https://yygh-zjut.oss-cn-hangzhou.aliyuncs.com/76f62cf207906149ba614837eee3ae6e.jpg",
          nickname: "Xzh",
          name: "徐振华",
          sex: "男",
          grade: "2020",
          major: "数字媒体与技术",
          states: 0,
          email: "718457605@qq.com",
          qq: "718457605",
          telephone: "18767175883",
          illegalcount: 6,
        },
      ],
    };
  },

  components: {},

  computed: {},

  mounted() {
    this.searchStudent(this.currentPage, this.limit, this.searchStudentVo);
  },

  methods: {
    changeSize(size) {
      this.limit = size;
      this.getStudentList(1);
    },
    getStudentList(page = 1) {},
    //将学生禁言
    banStudent(studentid) {
      this.$alert("您确定禁言该用户吗", "禁言详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            adminControll.updateIsForbidden(0, studentid).then((response) => {
              this.searchStudent(
                this.currentPage,
                this.limit,
                this.searchStudentVo
              );
            });
          }
        },
      });
    },
    //解除学生禁言
    cancelBan(studentid) {
      this.$alert("您确定取消该用户的禁言吗", "取消详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            adminControll.updateIsForbidden(1, studentid).then((response) => {
              this.searchStudent(
                this.currentPage,
                this.limit,
                this.searchStudentVo
              );
            });
          }
        },
      });
    },
    //赋予学生管理员权限
    changeToAdmin(studentid) {
      this.$alert("您确定将该用户设置为管理员吗", "权限详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            adminControll.updateIsAdmin(1, studentid).then((response) => {
              this.searchStudent(
                this.currentPage,
                this.limit,
                this.searchStudentVo
              );
            });
          } else if (action == "cancel") {
          }
        },
      });
    },
    //撤销学生管理员权限
    changeToNormal(studentid) {
      this.$alert("您确定撤回该用户的管理员权限吗", "权限详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            adminControll.updateIsAdmin(0, studentid).then((response) => {
              this.searchStudent(
                this.currentPage,
                this.limit,
                this.searchStudentVo
              );
            });
          }
        },
      });
    },
    //搜索学生信息
    searchStudent(currentPage, limit, searchStudentVo) {
      adminControll
        .adminSearchStudent(currentPage, limit, searchStudentVo)
        .then((response) => {
          console.log("学生搜索请求发送成功");
          let studentList = response.data.records;
          this.studentList = studentList;
          this.currentPage = response.data.current;
          this.totalRecords = response.data.total;
        });
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.searchStudent(this.currentPage, this.limit, this.searchStudentVo);
    },
  },
};
</script>
<style scoped>
img {
  display: block;
  width: 75px;
  height: 75px;
}
.exceed {
  color: red;
}
</style>
